Source, information, and implementation

The data set was sourced from the World Bank website and was downloaded specifically as a csv. Some clean up in excel was performed to simplify the read command in R but if this data was updated daily (like stock data), this would have been done within R as a function. Several empty rows near the header were deleted and all the years previous to 2001 were deleted. To replicate this work, the link can be followed to download the CSV file used. From there, delete all the rows preceding the main header used for the data set (i.e Country and years). Finally delete all the year columns before 2001.

The information that is being conveyed is showing the rapid rise of GDP between China, the EU, and US. Other major countries are also included to demonstrate that their GDP has been more or less consistent. Plotly was used exclusively in this assignment.

As far as features, 2 were used for this plot. The first feature is the range slider at the bottom of the plot. This would be of more use if the data set was more granular in its dates but it can still be used to isolate specific sections of the x-axis. The second feature used was a drop down menu to pre-select different countries as an alternative to the legend. The full capability of this function was not explored but can potentially allow for a drop down menu that will switch the type of plot being demonstrated (e.g scatter vs box plot). Additionally, if more countries were included, it could be beneficial to use the drop down menu as a means to isolate specific regions (e.g North America, South America, East Asian, ect.) and this plot can easily be expanded for this purpose.

Code

#Importing Library and data set
library(tidyverse)
library(plotly)
raw_data <- read.csv("Country_GDP.csv")

Filter, extract, and clean relevant data

c_code <- c("USA","EUU","CHN","JPN","CAN","BRA")
filt_data <- filter(raw_data,Country.Code %in% c_code)
colnames(filt_data) <- col_nam <- c("Country",
                                        "Country_Code",seq(2001,2020))

plot_data <- data.frame(t(filt_data[,3:22]))
colnames(plot_data) <- filt_data$Country_Code


#GDP Per Billion dollars
plot_data <- plot_data/1000000000
head(plot_data)
##           BRA      CAN     CHN      EUU     JPN     USA
## 2001  559.984  738.982 1339.40  7388.17 4374.71 10581.8
## 2002  509.795  760.649 1470.55  8050.02 4182.85 10936.4
## 2003  558.234  895.541 1660.29  9912.55 4519.56 11458.2
## 2004  669.289 1026.690 1955.35 11400.60 4893.12 12213.7
## 2005  891.634 1173.110 2285.97 11907.00 4831.47 13036.6
## 2006 1107.630 1319.260 2752.13 12704.70 4601.66 13814.6

Creating the plot

fig <- plot_ly(plot_data, type = 'scatter', mode = 'lines') %>%
  add_trace(x = seq(2001,2020), y = ~BRA, name = 'Brazil') %>%
  add_trace(x = seq(2001,2020), y = ~CAN, name = 'Canada') %>%
  add_trace(x = seq(2001,2020), y = ~CHN, name = 'China') %>%
  add_trace(x = seq(2001,2020), y = ~EUU, name = 'EU') %>%
  add_trace(x = seq(2001,2020), y = ~JPN, name = 'Japan') %>%
  add_trace(x = seq(2001,2020), y = ~USA, name = 'USA') %>%
  layout(title = "Major Countries GDP",
         legend = list(title=list(text='Countries'),x=-0.3,y=0), 
         xaxis = list(title = 'Date',
                      rangeslider = list(visible = T)),
         yaxis = list(title = 'GDP [$Billions]'),
         plot_bgcolor='#e5ecf6',
         
         updatemenus = list(
           list(
             buttons = list(
               list(method = 'restyle',
                    args = list('visible',list(TRUE,TRUE,TRUE,TRUE,TRUE,TRUE)),
                    label = "All"),
               
               list(method = 'restyle',
                    args = list('visible',list(TRUE,FALSE,FALSE,TRUE,TRUE,FALSE)),
                    label = "Super Powers"),
               
               list(method = 'restyle',
                    args = list('visible',list(TRUE,FALSE,TRUE,FALSE,FALSE,FALSE)),
                    label = "North America")))
           ))

fig